package thinking.in.spring.ioc.bean.lifecycle;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

/**
 * BeanDefinition 合并示例
 */
public class MergedBeanDefinitionDemo
{

    public static void main(String[] args)
    {
        // TODO
        // XmlBeanDefinitionReader 加载XML Bean 通过User 和 SuperUser来做示例

        // 调试方法 getMergedBeanDefinition（）

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        String location = "META-INF/dependency-lookup-context.xml";
        Resource resource = new ClassPathResource(location);
        EncodedResource encodedResource = new EncodedResource(resource, "utf-8");
        int count = reader.loadBeanDefinitions(encodedResource);
        System.out.println(beanFactory.getBean("user"));
        System.out.println(beanFactory.getBean("superUser"));
        System.out.println("已加载 BeanDefinition 数量：" + count);
    }
}
